<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>异常日志</title>
    <div th:replace="common/base"></div>
    <style>
        .ui-state-highlight-coolcolor {
            border: 1px solid #8BBFF4;
            color: #363636;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <div style="margin: 0px 10px">
            <form class="layui-form layui-form-pane" action="">
                <div class="layui-form-item" style="margin: 0px">
                    <div class="layui-inline">
                        <label class="layui-form-label">关键字</label>
                        <div class="layui-input-inline">
                            <input type="text" name="keyword" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" type="submit" class="layui-btn layui-btn-primary" lay-submit
                                lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                        </button>
                    </div>
                </div>
            </form>
        </div>

        <script type="text/html" id="toolbarLog">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"><i
                        class="layui-icon">&#xe640;</i>清空
                </button>
            </div>
        </script>

        <table id="logTable" lay-filter="logTable"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-green layui-btn-xs data-count-preview" lay-event="preview">&nbsp;&nbsp;查看&nbsp;&nbsp;</a>
        </script>
    </div>
</div>
<!-- 表单弹窗 -->
<script type="text/html" id="logDialog">
    <div id="errorLogFrm" style="font-size: 14px">
        <div class="ui-state-highlight-coolcolor" style="padding:5px; margin:5px;border-style:dotted;">
            <b>【IP】</b>
            <span id="eIp" style="word-wrap: break-word;"></span>
        </div>
        <div class="ui-state-highlight-coolcolor" style="padding:5px; margin:5px;border-style:dotted;">
            <b>【添加时间】</b>
            <span id="eAddTime" style="word-wrap: break-word;"></span>
        </div>
        <div class="ui-state-highlight-coolcolor" style="padding:5px; margin:5px;border-style:dotted;">
            <b>【请求方法】</b>
            <span id="eMethodName" style="word-wrap: break-word;"></span>
        </div>
        <div class="ui-state-highlight-coolcolor" style="padding:5px; margin:5px;border-style:dotted;">
            <b>【请求参数】</b>
            <span id="eParameter" style="word-wrap: break-word;"></span>
        </div>
        <div class="ui-state-highlight-coolcolor" style="padding:5px; margin:5px;border-style:dotted;">
            <b>【异常类型】</b>
            <span id="eExceptionType" style="word-wrap: break-word;"></span>
        </div>
        <div class="ui-state-highlight-coolcolor" style="padding:5px; margin:5px;border-style:dotted;">
            <b>【异常消息】</b>
            <span id="eExceptionMsg" style="word-wrap: break-word;"></span>
        </div>

    </div>
</script>
<div th:replace="common/baseJS"></div>
<script th:inline="none">
    layui.use(['form', 'table', 'ab'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            ab = layui.ab;

        table.render({
            elem: '#logTable',
            url: '/monitor/log/findPage?logType=ERROR',
            toolbar: '#toolbarLog',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {type: 'numbers'},
                {field: 'id', width: 80, title: 'ID', hide: true},
                {field: 'username', title: '用户名'},
                {field: 'requestIp', title: 'IP'},
                {field: 'address', title: 'IP来源'},
                {field: 'description', title: '描述'},
                {field: 'browser', title: '浏览器'},
                {
                    field: 'time', title: '请求耗时', templet: function (d) {
                        return '<span class="layui-badge layui-bg-blue">' + d.time + 'ms</span>';
                    }
                },
                {
                    field: 'createTime', title: '创建时间', templet: function (d) {
                        if (d.createTime != null) {
                            return ab.dateUtil.format(d.createTime, "yyyy-MM-dd hh:mm:ss");
                        }
                        return '';
                    }
                },
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            response: {
                statusName: 'status', //规定数据状态的字段名称，默认：code
                statusCode: 200, //规定成功的状态码，默认：0
                msgName: 'hint', //规定状态信息的字段名称，默认：msg
                countName: 'total', //规定数据总数的字段名称，默认：count
                dataName: 'rows' //规定数据列表的字段名称，默认：data
            },
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('logTable', {
                page: {curr: 1},
                where: data.field
            }, 'data');

            return false;
        });
        //自定义验证规则
        form.verify({
            name: function (value) {
                if (value.length < 2) {
                    return '模块名称至少得2个字符啊';
                }
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(logTable)', function (obj) {
            if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('logTable')
                    , data = checkStatus.data;
                if (data.length == 0) {
                    ab.window.msg("请至少选择一条数据！", "error");
                    return;
                }
                var ids = [];
                for (var i = 0; i < data.length; i++) {
                    ids.push(data[i].id);
                }
                ab.util.deleteForm({
                    url: "/monitor/log",// 请求地址
                    type: "delete",// 请求提交方式
                    data: JSON.stringify(ids),// 发送回去的数据 JSON.stringify(data)
                    success: function (data) {
                        $("#btnSearch").click();
                    }
                });
            }
        });

        /* 表格操作列点击事件 */
        table.on('tool(logTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'preview') {
                var loadingIdx = layer.load(2);
                ab.ajax({
                    url: '/monitor/log/get',// 请求地址
                    type: "get",// 请求提交方式
                    data: {id: data.id},// 发送回去的数据 JSON.stringify(data)
                    success: function (data) {
                        showEditModel(data);
                        layer.close(loadingIdx);
                    },
                    error: function () {
                        layer.close(loadingIdx);
                    }
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(logTable)', function (obj) {
            // console.log(obj)
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            ab.window.open({
                id: "addLog",
                title: '异常消息',
                content: $("#logDialog").html(),//"/system/log/form",
                width: "980px",
                height: "600px",
                btn: ['　关闭　'],
                success: function (layero, dIndex) {
                    $("#eIp").html(mData.requestIp);
                    if (mData.createTime != null) {
                        $("#eAddTime").html(ab.dateUtil.format(mData.createTime, "yyyy-MM-dd hh:mm:ss"));
                    }
                    $("#eClassName").html(mData.className);
                    $("#eMethodName").html(mData.method);
                    $("#eParameter").html(mData.params);
                    $("#eExceptionType").html(mData.exceptionType);
                    $("#eExceptionMsg").html(mData.exceptionDetail);
                    // 禁止弹窗出现滚动条
                    //$(layero).children('.layui-layer-content').css('overflow', 'visible');
                },
                callback: function (layerId, dIndex) {
                    layer.close(dIndex);
                }
            });
        }
    });
</script>

</body>
</html>